package algorithm

/*
return val: -1 not found
Notice: middle overflow
        注意 >> 优先级

unintest: http://blog.csdn.net/drzhouweiming/article/details/1562717
*/
func BinarySearch(data []int, val int) int {
	left := 0
	right := len(data) - 1
	middle := 0

	//fmt.Printf("=========%v %d\n", data, val)
	for left <= right {
		middle = left + ((right - left) >> 1)
		//fmt.Printf("=========%d %d %d\n", left, middle, right)
		if data[middle] > val {
			right = middle - 1
		} else if data[middle] < val {
			left = middle + 1
		} else {
			return middle
		}
	}

	return -1
}
